//
//  HChangeStandardViewController.m
//  NursingManages
//
//  Created by Zhen Huang on 17/5/26.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HChangeStandardViewController.h"
#import "TableQcStandard.h"
#import "HPushViewDelegate.h"
#import <TMCache.h>
@interface HChangeStandardViewController ()
<UITableViewDelegate,
UITableViewDataSource,
HPushViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *openType;
@property (nonatomic, strong)NSDictionary *menuParam;

@end

@implementation HChangeStandardViewController

-(instancetype)initWithOpenType:(NSString *)OpenType
                      menuParam:(NSDictionary *)menuParam
{
    if(self){
        self.menuParam  = menuParam;
        self.typeArray = [[[TableQcStandard alloc]init]GetQcTypeWithType_id:[menuParam objectForKey:@"TYPE_ID"]];
        self.standardArray = [NSMutableArray new];
        self.openType = OpenType;
    
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    for (HQcTypeModel *model in self.typeArray) {
        NSMutableArray *array = [NSMutableArray new];
        array = [[[TableQcStandard alloc]init] GetQcStandardWithType_id:model.TYPE_ID];
        [self.standardArray addObject:array];
    }
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = kNavBlueColor;
    self.navigationController.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]};
    [self.view addSubview:self.tableView];
    
}

-(void)CallBack:(NSObject *)object{
    //  在 HQualityViewController.m 中执行代理，本方法其实可以注释掉
}

- (UITableView *)tableView
{
    if (!_tableView) {
        CGRect rect = CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight);
        if ([self.openType isEqualToString:@"presentView"]) {
            rect = CGRectMake(0, 0, ScreenWidth, ScreenHeight );
        }
        _tableView = [[UITableView alloc]initWithFrame:rect style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.tableFooterView = [[UIView alloc]init];
        
    }
    return _tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.typeArray.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    HQcTypeModel *model = self.typeArray[section];
    return model.TYPE_NAME;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = self.standardArray[section];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *array = self.standardArray[indexPath.section];
    HQc_standardModel *model = array[indexPath.row];
    cell.textLabel.text = model.STANDARD_NAME;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array = self.standardArray[indexPath.section];
    HQc_standardModel *model = array[indexPath.row];
    
    if([self.delegate respondsToSelector:@selector(CallBack:)]){
        if ([self.openType isEqualToString:@"pushView"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [self dismissViewControllerAnimated:YES completion:NULL];
        }
        [self.delegate CallBack:model];
        [[TMCache sharedCache]removeObjectForKey:@"depart"];

    }
    
}


@end
